package chaowan.Service.impl;
import chaowan.Mapper.OrderMainMapper;
import chaowan.Mapper.ShopUserMapper;
import chaowan.Service.ShopUserService;
import chaowan.pojo.ShopUser;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ShopUserServiceImpl implements ShopUserService {

    @Resource
    private ShopUserMapper shopUserMapper;

    @Override
    public List<ShopUser> getAllUsers() {
        return shopUserMapper.selectAll();
    }

    @Override
    public ShopUser getUserById(Long uid) {
        return shopUserMapper.selectById(uid);
    }

    @Override
    public int save(ShopUser user) {
        return shopUserMapper.insert(user);
    }

    @Override
    public int update(ShopUser user) {
        return shopUserMapper.update(user);
    }

    @Autowired
    private OrderMainMapper orderMainMapper;


    @Override
public ShopUser login(String uname, String pwd) {
    return shopUserMapper.findByUsernameAndPassword(uname, pwd);
}
  @Override
    public int register(ShopUser user) {
        ShopUser existing = shopUserMapper.findByUsername(user.getUname());
        if (existing != null) {
            return 0; 
        }
        return shopUserMapper.insertUser(user);
    }
      public ShopUser findByUsername(String uname) {
        return shopUserMapper.findByUsername(uname);
    }
     @Override
    public boolean deleteById(Long uid) {
        int count = orderMainMapper.countByUserId(Math.toIntExact(uid));
        if (count > 0) {
            throw new RuntimeException("该用户存在订单，不能删除");
        }
        return shopUserMapper.deleteById(Long.valueOf(uid)) > 0;
    }

}